package com.tistory.admin.common.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;


@SuppressWarnings("serial")
public final class TextareaToLineFillterTag extends TagSupport {

	/** 조회_Key */
	private String value;
	private String escapXml;
	private Boolean brTag;
	public static final int HIGHEST_SPECIAL = '>';
	public static char[][] specialCharactersRepresentation = new char[HIGHEST_SPECIAL + 1][];
	static {
		specialCharactersRepresentation['&'] = "&amp;".toCharArray();
		specialCharactersRepresentation['<'] = "&lt;".toCharArray();
		specialCharactersRepresentation['>'] = "&gt;".toCharArray();
		specialCharactersRepresentation['"'] = "&#034;".toCharArray();
		specialCharactersRepresentation['\''] = "&#039;".toCharArray();
	}

	/** TextareaToLineFillterTag */
	public TextareaToLineFillterTag() {
		super();
	}

	@Override
	public int doStartTag() throws JspException {
		try {
			String result = "";
			String conVerWord = "<br/>";

			if (brTag == null) {
				brTag = true;
			}

			if (value != null) {
				if (escapXml.equalsIgnoreCase("true")) {
					value = escapXml(value);
				}
				if (brTag) {
					value = value.replaceAll("\n", conVerWord).replaceAll("\r\n", conVerWord);
				}
				result = value;
			}

			pageContext.getOut().print(result);

		} catch (Exception e) {
//			OmpLogger.error(e.getMessage(), e);
		}
		return SKIP_BODY;
	}

	public String escapXml(String value) {
		if (value == null)
			return "";
		return escapeXml(value);
	}

	/**
     * Performs the following substring replacements
     * (to facilitate output to XML/HTML pages):
     *
     *    & -> &amp;
     *    < -> &lt;
     *    > -> &gt;
     *    " -> &#034;
     *    ' -> &#039;
     *
     * See also OutSupport.writeEscapedXml().
     */
	public static String escapeXml(String buffer) {
		int start = 0;
		int length = buffer.length();
		char[] arrayBuffer = buffer.toCharArray();
		StringBuffer escapedBuffer = null;

		for (int i = 0; i < length; i++) {
			char c = arrayBuffer[i];
			if (c <= HIGHEST_SPECIAL) {
				char[] escaped = specialCharactersRepresentation[c];
				if (escaped != null) {
					// create StringBuffer to hold escaped xml string
					if (start == 0) {
						escapedBuffer = new StringBuffer(length + 5);
					}
					// add unescaped portion
					if (start < i) {
						escapedBuffer.append(arrayBuffer, start, i - start);
					}
					start = i + 1;
					// add escaped xml
					escapedBuffer.append(escaped);
				}
			}
		}
		// no xml escaping was necessary
		if (start == 0) {
			return buffer;
		}
		// add rest of unescaped portion
		if (start < length) {
			escapedBuffer.append(arrayBuffer, start, length - start);
		}
		return escapedBuffer.toString();
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getEscapXml() {
		return escapXml;
	}

	public void setEscapXml(String escapXml) {
		this.escapXml = escapXml;
	}

	public Boolean getBrTag() {
		return brTag;
	}

	public void setBrTag(Boolean brTag) {
		this.brTag = brTag;
	}

}
